package kite.springcloud.stream.kafka.producer.controller;

import kite.springcloud.common.stream.ShopChannelProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * ShopServiceController
 *
 * @author fengzheng
 * @date 2018/12/14
 */
@Slf4j
@RestController
public class ShopServiceController {

    @Resource(name = ShopChannelProcess.SHOP_TOPIC_OUTPUT)
    private MessageChannel sendShopMessageChannel;

    @GetMapping("/sendShopMsg")
    public String sendShopMessage(String content) {
        log.info("发送Shop消息内容:" + content);
        Message<String> message = MessageBuilder.withPayload(content)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build();
        boolean isSendSuccess = sendShopMessageChannel.send(message);
        log.info("isSendSuccess={},send message={}", isSendSuccess, message);
        return isSendSuccess ? "shop消息发送成功" : "shop消息发送失败";
    }

}